$(document).ready(function () {
    calendar(0);
});

var cDate=new Date();
var month=cDate.getMonth();
var year=cDate.getFullYear();
var nowDate=new Date();
var globalObj;//指向当前日历被点击的标签
// 触发莫泰的点击事件
function setCal(obj){
    globalObj=obj;
    $("#modalClick").click();
}

/**
 * 设置价格日历的价格
 */

function getInfo(obj){
    $stock=$("#stock").val();
    $price=$("#price").val();
    if($price<=0){
        alert('价格值大于零');
        return;
    }else if($price%$price!=0){
        alert('请输入数字');
        return;
    }

    if ($price<=0){
        alert('库存值大于零');
        return;
    }else if ($price%$price!=0){
        alert('请输入数字');
        return;
    }
    $(globalObj).children('font.price').html($price+'￥');
    $(globalObj).children('font.store').html($stock + '件');
    $(globalObj).children('input[name="price"]').val($price);
    $(globalObj).children('input[name="stock"]').val($stock);
    $("#myModal").modal('hide');
    // $(globalObj).text($stock)
    $date=$(globalObj).children('input[name="c_date"]').val();
    goods_id=$("input[name='goods_id']").val();
//        form_type=$("input[name='form_type']").val();
//     if (form_type=='scenic'){//判断是景区商品类型
//         sku_id=$("select[name='scenic_type']").val();
//     }else if(form_type=='hotel'){//判断是酒店商品类型
//         sku_id=$("select[name='roomtype']").val();
//     }else{
//         tip.msgbox.err('保存数据异常，请刷新页面后重试!');
//         return;
//     }

//        //alert($form_type);
    //异步保存价格日历信息
    var data={"price":$price,"stock":$stock,"goods_id":goods_id,
        "sku_id":sku_id,"pdate":$date,"form_type":form_type};

    $.post('{php echo webUrl("goods/updateCalendarPrice")}',data,function (data) {
        $("#myModal").modal('hide');
        //console.log(data);
        data=JSON.parse(data)
        $("#err_msg").html(data);
//            //alert(data.status);
        if (data.status==1)
            tip.msgbox.suc('更新成功！');
        else
            tip.msgbox.err('更新失败！');
    });
}

/**
 * 生成日历
 * @param act 表示获取的月份 0当月，-1前一月，1下一月
 */
function  calendar(act) {
    if (act==0){
        cDate=new Date();
        month=cDate.getMonth();
        year=cDate.getFullYear();
    }else{
        if(act>1){
            cDate=new Date();
            month=cDate.getMonth();
            year=cDate.getFullYear();
        }
        month+=act;
    }

    if (month==12){
        year+=1;
        month=0;
    }else if(month==-1){
        year-=1;
        month=11;
    }
    var firstDate=new Date(year,month,1);
    $week=firstDate.getDay();
    $week=$week==0?7:$week;
    var startDate=new Date(year,month,1);
    startDate.setDate(startDate.getDate()-$week);


    $strHeard='<table class="table table-bordered">';
    $strTd='<tr>\n' +
        '        <th>星期一</th>\n' +
        '        <th>星期二</th>\n' +
        '        <th>星期三</th>\n' +
        '        <th>星期四</th>\n' +
        '        <th>星期五</th>\n' +
        '        <th>星期六</th>\n' +
        '        <th>星期天</th>\n' +
        '    </tr>';
    for ($i=1;$i<42;){
        $strTd+='<tr>';
        for ($j=0;$j<7;$j++,$i++){

            startDate.setDate(startDate.getDate()+1);
            $Fulldate=startDate.getFullYear()+'-';
            $FullM=(startDate.getMonth()+1)>=10?(startDate.getMonth()+1):'0'+(startDate.getMonth()+1);
            $FullD=startDate.getDate()>=10?startDate.getDate():'0'+startDate.getDate();
            $Fulldate=$Fulldate+$FullM+'-'+$FullD;
            if (startDate.getMonth() == nowDate.getMonth()) {
                $price=[{"goodsid":18,"skuid":512,"pdate":"2017-10-11","price":"360.00","store":-1,"common_Store":1},{"goodsid":18,"skuid":512,"pdate":"2017-10-12","price":"360.00","store":-1,"common_Store":1},{"goodsid":18,"skuid":512,"pdate":"2017-10-13","price":"360.00","store":-1,"common_Store":1},{"goodsid":18,"skuid":512,"pdate":"2017-10-14","price":"360.00","store":-1,"common_Store":1},{"goodsid":18,"skuid":512,"pdate":"2017-10-15","price":"360.00","store":-1,"common_Store":1},{"goodsid":18,"skuid":512,"pdate":"2017-10-16","price":"360.00","store":-1,"common_Store":1},{"goodsid":18,"skuid":512,"pdate":"2017-10-17","price":"360.00","store":-1,"common_Store":1},{"goodsid":18,"skuid":512,"pdate":"2017-10-18","price":"360.00","store":-1,"common_Store":1},{"goodsid":18,"skuid":512,"pdate":"2017-10-19","price":"360.00","store":-1,"common_Store":1},{"goodsid":18,"skuid":512,"pdate":"2017-10-20","price":"360.00","store":-1,"common_Store":1},{"goodsid":18,"skuid":512,"pdate":"2017-10-21","price":"360.00","store":-1,"common_Store":1},{"goodsid":18,"skuid":512,"pdate":"2017-10-22","price":"360.00","store":-1,"common_Store":1},{"goodsid":18,"skuid":512,"pdate":"2017-10-23","price":"360.00","store":-1,"common_Store":1},{"goodsid":18,"skuid":512,"pdate":"2017-10-24","price":"360.00","store":-1,"common_Store":1},{"goodsid":18,"skuid":512,"pdate":"2017-10-25","price":"360.00","store":-1,"common_Store":1},{"goodsid":18,"skuid":512,"pdate":"2017-10-26","price":"360.00","store":-1,"common_Store":1},{"goodsid":18,"skuid":512,"pdate":"2017-10-27","price":"360.00","store":-1,"common_Store":1},{"goodsid":18,"skuid":512,"pdate":"2017-10-28","price":"360.00","store":-1,"common_Store":1},{"goodsid":18,"skuid":512,"pdate":"2017-10-29","price":"360.00","store":-1,"common_Store":1},{"goodsid":18,"skuid":512,"pdate":"2017-10-30","price":"360.00","store":-1,"common_Store":1},{"goodsid":18,"skuid":512,"pdate":"2017-10-31","price":"360.00","store":-1,"common_Store":1}];
                $toDay=getNowPrice($price,$Fulldate);

                if ($toDay==false){
//                         console.log('toDayValue'+$toDay);
                    $toDayPrice='';
                    $toDayStore='';
                }else {
                    console.log('toDayValue'+$toDay);
                    $toDayPrice=$toDay.price+'￥';
                    if ($toDay.store==-1)
                        $toDayStore='充足';
                    else $toDayStore=$toDay.store+'件';

                }

//                $toDay==false?'':alert('toDay'+$toDay.price);
                if (startDate.getDate() == nowDate.getDate()) {
                    $strTd += '<td onclick="setCal(this)"><font color="red" style="font-family: Arial Black">' + startDate.getDate() + '</font><br><font color="red" class="price">'+$toDayPrice+'</font><br><font class="store">'+$toDayStore+'</font><input type="hidden" name="price"><input type="hidden" name="stock"><input type="hidden" name="date" value="' + $Fulldate + '"></td>';
                    continue;
                }
                $strTd += '<td onclick="setCal(this)"><font style="font-family: Arial Black">' + startDate.getDate() + '</font><br><font color="red" class="price">'+$toDayPrice+'</font><br><font class="store">'+$toDayStore+'</font><input type="hidden" name="price"><input type="hidden" name="stock"><input type="hidden" name="date" value="' + $Fulldate + '"></td>';
                continue;
            }
            $strTd+='<td onclick=""><font>'+startDate.getDate()+'</font><br><font></font><font></font><input type="hidden" name="price"><input type="hidden" name="stock"><input type="hidden" name="date" value="'+$Fulldate+'"></td>';

        }
        $strTd+='</tr>';
    }
    $strHeard+=$strTd+'</table>';

    $calendarHead='    <table class="table table-striped">\n' +
        '        <tr>\n' +
        '            <td style="width: 10%">\n' +
        '                    <button type="button" class="btn btn-primary btn-lg btn-block" onclick="calendar(-1)">&lt;&lt;</button></td><td style="width: 10%">\n' +
        '                    <button type="button" class="btn btn-primary btn-lg btn-block" onclick="calendar(1)">&gt;&gt;</button></td><td style="width: 20%">\n' +
        '                    <button type="button" class="btn btn-primary btn-lg btn-block" onclick="calendar(0)">今天</button></td><td>\n' +
        '            </td>\n' +
        '            <td><button type="button" class="btn btn-primary btn-lg btn-block">'+firstDate.getFullYear()+'年'+(firstDate.getMonth()+1)+'月'+nowDate.getDate()+'日</button></td>\n' +
        '        </tr>\n' +
        '    </table>';
    $("#calendar").html($calendarHead+$strHeard);

}
//获取指定日期的价格日历数据
function getNowPrice(priceList,pdate){
    var toDay=false;
    console.log(priceList);
    $.each(priceList,function (key,item) {

//            console.log(item.pdate+'=='+pdate);
        if (item.pdate==pdate){
//                console.log(item.pdate+'=='+pdate+'--'+(item.pdate==pdate));
//                alert(p.pdate)
            toDay=item;
            return;
        }
    })
    return toDay;

}
//设置一个选项
// function setPrice(){
//     $price=prompt('请输入价格',0);
//     if($price==0){
//         alert('价格不能为零');
//         return;
//     }else if($price%$price!=0){
//         alert('请输入数字');
//         return;
//     }
//     $actObj=$($obj).children(':first').next();
//     $actObj.next().html($price+'元');
//     $actObj.next().next().val($price);
// }
